package com.jm.weibo.logic;

import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import weibo4andriod.Comment;
import weibo4andriod.DirectMessage;
import weibo4andriod.Paging;
import weibo4andriod.Status;
import weibo4andriod.Trends;
import weibo4andriod.User;
import weibo4andriod.Weibo;
import weibo4andriod.WeiboException;
import weibo4andriod.http.AccessToken;
import weibo4andriod.http.ImageItem;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.Toast;

import com.jm.weibo.db.OAuthReadUtil;
import com.jm.weibo.db.SaveUserLoginParam;
import com.jm.weibo.ui.ListAllFriendsActivity;
import com.jm.weibo.ui.ListMyFansActivity;
import com.jm.weibo.ui.ListMyTopicsActivity;
import com.jm.weibo.ui.ListMyWeibosActivity;
import com.jm.weibo.ui.MyListActivity;
import com.jm.weibo.ui.LoginActivity;
import com.jm.weibo.ui.MessageActivity;
import com.jm.weibo.ui.NewWeiboActivity;
import com.jm.weibo.ui.R;
import com.jm.weibo.ui.UserInfoActivity;
import com.jm.weibo.util.LoadImageUtil;
import com.jm.weibo.util.NetUtil;

//系统调度模块
public class MainService extends Service implements Runnable {

	public static boolean isrun = false;

	public static User currentUser = null;

	public static MainService mainService;// 全局静态引用

	public NetUtil netReceiver = new NetUtil();

	// 保存所有任务对象
	private static ArrayList<Task> allTask = new ArrayList<Task>();

	// 定义微博处理对象
	public Weibo weibo = new Weibo();

	// 保存所有 Activity
	public static ArrayList<IWeiboActivity> allActivity = new ArrayList<IWeiboActivity>();

	// 保存所有的用户头像
	public static HashMap<Integer, BitmapDrawable> allIcon = new HashMap<Integer, BitmapDrawable>();
	// 定义错误信息
	public int weiboerror = 100;

	// 用来存放图片的容器
	public static Map<Integer, BitmapDrawable> allImage = new HashMap<Integer, BitmapDrawable>();

	public static void newTask(Task task) {
		allTask.add(task);
	}

	public MainService() {
		System.out.println("----MainService()被调用了---");
		mainService = this;
	}

	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 根据Activity获得该Activity的实例
	 * 
	 * @param name
	 *            Activity的名字
	 * @return 返回该Activity的实例
	 */
	public static IWeiboActivity getActivityByName(String name) {
		IWeiboActivity ia = null;
		for (IWeiboActivity ac : allActivity) {
			if (ac.getClass().getName().indexOf(name) >= 0) {
				ia = ac;
			}
		}
		return ia;
	}

	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		mainService = this;
		Thread t = new Thread(this);
		t.start();
		// 添加网络状态变化的广播接收器
		this.registerReceiver(netReceiver, new IntentFilter(
				"android.net.conn.CONNECTIVITY_CHANGE"));
	}

	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
	}

	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	public void run() {
		// TODO Auto-generated method stub
		if (weibo == null) {
			weibo = new Weibo();
			Toast.makeText(this, "微博为空", Toast.LENGTH_SHORT);
		}

		while (isrun) {
			Task lasttask = null;
			Log.d("core Logic", "..............run");
			synchronized (allTask) {// 接任务
				if (allTask.size() > 0) {
					lasttask = allTask.get(0);
				}
				// 执行任务
				doTask(lasttask);
			}
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
			}
		}
	}

	// 执行任务
	public void doTask(Task task) {

		Message message = hand.obtainMessage();
		try {
			switch (task.getTaskID()) {
			case Task.TASK_USER_LOGIN:// 用户登录
				weibo.setUserId((String) task.getTaskParam().get("user"));
				weibo.setPassword((String) task.getTaskParam().get("pass"));
				String result = "";
				try {
					currentUser = weibo.verifyCredentials();
					// 保存用的登陆数据
					SaveUserLoginParam.saveUserLoginParam(this, (String) task
							.getTaskParam().get("user"), (String) task
							.getTaskParam().get("pass"));

					result = "loginok";
				} catch (Exception e) {
					result = "loginerr";
				}
				message.obj = result; // 保存登录结果
				break;

			case Task.TASK_GET_USER_HOMETIMEINLINE:// 获取用户首页微博信息
				int nowpage = (Integer) task.getTaskParam().get("nowpage");
				int pagesize = (Integer) task.getTaskParam().get("pagesize");
				Paging p = new Paging(nowpage, pagesize);// 创建分页对象
				List<Status> alls = weibo.getFriendsTimeline(p);

				for (Status st : alls) { // 该用户的头像没有获取过
					if (allIcon.get(st.getUser().getId()) == null) {
						HashMap param = new HashMap();
						param.put("uid", st.getUser().getId());
						param.put("url", st.getUser().getProfileImageURL());
						Log.d("user", st.getUser().getId() + "-----"
								+ st.getUser().getProfileImageURL());
						Task ts = new Task(Task.TASK_GET_USER_IMAGE_ICON, param);
						MainService.addNewTask(ts);
					}
				}
				message.obj = alls;
				break;
			case Task.TASK_GET_USER_HOMETIMEINLINE_MORE:// 获取更多用户首页微博信息
				int nowpage2 = (Integer) task.getTaskParam().get("nowpage");
				int pagesize2 = (Integer) task.getTaskParam().get("pagesize");
				Paging p2 = new Paging(nowpage2, pagesize2);// 创建分页对象
				List<Status> alls2 = weibo.getFriendsTimeline(p2);
				for (Status st : alls2) { // 该用户的头像没有获取过
					if (allIcon.get(st.getUser().getId()) == null) {
						HashMap param = new HashMap();
						param.put("uid", st.getUser().getId());
						param.put("url", st.getUser().getProfileImageURL());
						Log.d("user", st.getUser().getId() + "-----"
								+ st.getUser().getProfileImageURL());
						Task ts = new Task(Task.TASK_GET_USER_IMAGE_ICON, param);
						MainService.addNewTask(ts);
					}

				}
				message.obj = alls2;
				break;

			case Task.TASK_GET_USER_IMAGE_ICON:
				URL url = (URL) task.getTaskParam().get("url");
				// 从网络获取用户的头像
				BitmapDrawable bd = NetUtil.getImageFromURL(url);
				// 得到头像对应的UID
				Integer uid = (Integer) task.getTaskParam().get("uid");
				// 添加到集合
				allIcon.put(uid, bd);
				break;

			case Task.TASK_GET_WEIBO_COMMENT:// 获取所有微博评论信息
				List<Comment> allc = weibo.getCommentsByMe();// 我发表的所有评论
				try {
					message.obj = allc;
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;

			case Task.TASK_GET_WEIBO_MESSAGE:// 获取所有私信
				List<DirectMessage> alldc = weibo.getDirectMessages();// 我所有私信
				message.obj = alldc;
				break;

			case Task.TASK_NEW_WEIBO_COMMENT:// 发表微博评论
				if ((Boolean) task.getTaskParam().get("news"))
					weibo.updateStatus((String) task.getTaskParam().get("msg"));
				weibo.updateComment((String) task.getTaskParam().get("msg"),
						(String) task.getTaskParam().get("id"), "");
				message.obj = "微博评论发表成功";
				break;

			case Task.TASK_NEW_WEIBO:// 发表普通微博信息
				try {
					Status st = weibo.updateStatus((String) task.getTaskParam()
							.get("msg"));
					message.obj = "微博发表成功";
				} catch (Exception e) {
					e.printStackTrace();
					message.obj = "可能是由于微博发表失败";
				}

				break;

			case Task.TASK_NEW_WEIBO_PIC:// 发表图片微博信息
				try {
					String msg = URLEncoder.encode((String) task.getTaskParam()
							.get("msg"), "UTF-8");
					ImageItem it = new ImageItem("pic", (byte[]) task
							.getTaskParam().get("picdat"));
					// 发表图片微博
					Status status = weibo.uploadStatus(msg, it);
					message.obj = "微博发表成功";
				} catch (Exception e) {
					message.obj = "可能是由于微博发表失败";
				}
				break;

			case Task.TASK_NEW_WEIBO_GPS:// 发表GPS微博信息
				try {
					// 发表图片微博
					Status statusgps = weibo
							.updateStatus(
									(String) task.getTaskParam().get("msg"),
									((double[]) task.getTaskParam().get(
											"gpspoint"))[0], ((double[]) task
											.getTaskParam().get("gpspoint"))[1]);
					message.obj = "微博发表成功";
				} catch (Exception e) {
					e.printStackTrace();
					message.obj = "可能是由于微博发表失败";
				}
				break;

			// weibo.updateStatus(status, inReplyToStatusId, latitude,
			// longitude)

			case Task.TASK_GET_WEIBO_IMAGE: // 加载微博中的图片
				System.out.println("-----加载微博中的图片--------");
				String urlStr = (String) task.getTaskParam().get("imageUrl");
				Drawable drawable = LoadImageUtil.loadIBitmapDrawable(urlStr);
				message.obj = drawable;

				// 得到头像对应的UID
				uid = (Integer) task.getTaskParam().get("uid");
				// 添加到集合
				allImage.put(uid, (BitmapDrawable) drawable);
				break;

			case Task.TASK_GET_USER_INFO: // 获取用户详细信息
				message.obj = MainService.currentUser;
				break;

			case Task.TAST_GET_ATTENTION_INFO: // 获取好像有的关注信息
				System.out.println("--获取好友有的关注信息 --");
				int nowpage4Attention = (Integer) task.getTaskParam().get(
						"nowpage4Attention");
				int pagesize4Attention = (Integer) task.getTaskParam().get(
						"pagesize4Attention");
				System.out.println("nowpage4Attention:" + nowpage4Attention);
				System.out.println("pagesize4Attention:" + pagesize4Attention);

				Paging paging = new Paging(nowpage4Attention,
						pagesize4Attention);
				List<User> attentionFriends = weibo.getFriendsStatuses(paging);
				message.obj = attentionFriends;
				break;
			// ??????????????????????????????????????????????????????
			case Task.TAST_GET_MORE_ATTENTION_INFO: // 获取更多的关注信息
				int nowpage4AttentionSec = (Integer) task.getTaskParam().get(
						"nowpage4Attention");
				int pagesize4AttentionSec = (Integer) task.getTaskParam().get(
						"pagesize4Attention");
				Paging pagingSec = new Paging(nowpage4AttentionSec,
						pagesize4AttentionSec);
				List<User> attentionFriendsSec = weibo
						.getFriendsStatuses(pagingSec);
				message.obj = attentionFriendsSec;
				break;

			case Task.TASK_GET_USER_IMAGE_ICON_ATTENTION:
				URL url2 = (URL) task.getTaskParam().get("url");
				// 从网络获取用户的头像
				BitmapDrawable bd2 = NetUtil.getImageFromURL(url2);
				// 得到头像对应的UID
				Integer uid2 = (Integer) task.getTaskParam().get("uid");
				// 添加到集合
				allIcon.put(uid2, bd2);
				break;

			case Task.TASK_GET_USER_IMAGE_ICON_FANS:
				URL url3 = (URL) task.getTaskParam().get("url");
				// 从网络获取用户的头像
				BitmapDrawable bd3 = NetUtil.getImageFromURL(url3);
				// 得到头像对应的UID
				Integer uid3 = (Integer) task.getTaskParam().get("uid");
				// 添加到集合
				allIcon.put(uid3, bd3);
				break;
			// ------------------罗洪州----------------
			case Task.TASK_GET_WEIBO_USER:
				// 获取我的微博列表
				int nowpage3 = (Integer) task.getTaskParam().get("nowpage");
				int pagesize3 = (Integer) task.getTaskParam().get("pagesize");
				Paging p3 = new Paging(nowpage3, pagesize3);// 创建分页对象
				List<Status> alls3 = weibo.getUserTimeline(p3);
				Status st = alls3.get(0);
				HashMap param = new HashMap();
				param.put("uid", st.getUser().getId());
				param.put("url", st.getUser().getProfileImageURL());

				Task ts = new Task(Task.TASK_GET_USER_IMAGE_ICON, param);
				MainService.addNewTask(ts);

				message.obj = alls3;
				break;

			case Task.TASK_GET_MORE_WEIBO_USER:
				int nowpage4 = (Integer) task.getTaskParam().get("nowpage");
				int pagesize4 = (Integer) task.getTaskParam().get("pagesize");
				Paging p4 = new Paging(nowpage4, pagesize4);// 创建分页对象
				List<Status> alls4 = weibo.getUserTimeline(p4);
				Status st1 = alls4.get(0);
				HashMap param1 = new HashMap();
				param1.put("uid", st1.getUser().getId());
				param1.put("url", st1.getUser().getProfileImageURL());

				Task ts1 = new Task(Task.TASK_GET_USER_IMAGE_ICON, param1);
				MainService.addNewTask(ts1);

				message.obj = alls4;
				break;

			// --------------------------------------------------------
			case Task.TASK_GET_MY_TOP:
	//			Response rp = new Response();
//				int nowpaget = (Integer) task.getTaskParam().get("nowpage");
//				int pagesizet = (Integer) task.getTaskParam().get("pagesize");
//				Paging pt = new Paging(nowpaget, pagesizet);// 创建分页对象
				List<Trends> allst = weibo.getDailyTrends();
				message.obj = allst;
				break;

			case Task.TASK_GET_MY_TOP_MORE:

				List<Trends> allstt = weibo.getDailyTrends();
				message.obj = allstt;
				break;
			// --------------------------------------------------------------
			case Task.TAST_GET_FANS_INFO: // 获取粉丝
				System.out.println("--获取粉丝 --");
				int nowpage4Fans = (Integer) task.getTaskParam().get(
						"nowpage4Fans");
				int pagesize4Fans = (Integer) task.getTaskParam().get(
						"pagesize4Fans");
				System.out.println("nowpage4Attention:" + nowpage4Fans);
				System.out.println("pagesize4Attention:" + pagesize4Fans);

				Paging paging4Fans = new Paging(nowpage4Fans, pagesize4Fans);
				List<User> attentionFriends4Fans = weibo
						.getFollowersStatuses(paging4Fans);
				message.obj = attentionFriends4Fans;
				break;
			// ?????????????????????????????
			case Task.TAST_GET_MORE_FANS_INFO: // 获取粉丝
				System.out.println("--获取更多de粉丝 --");
				int nowpage4MoreFans = (Integer) task.getTaskParam().get(
						"nowpage4Fans");
				int pagesize4MoreFans = (Integer) task.getTaskParam().get(
						"pagesize4Fans");

				Paging paging4MoreFans = new Paging(nowpage4MoreFans,
						pagesize4MoreFans);
				List<User> attentionFriends4MoreFans = weibo
						.getFollowersStatuses(paging4MoreFans);
				message.obj = attentionFriends4MoreFans;
				break;
			}

			message.what = task.getTaskID();

		} catch (WeiboException e) {
			message.what = e.getStatusCode();
		} catch (Exception es) {
			message.what = this.weiboerror;
		}

		allTask.remove(task);// 完成任务
		// 发送刷新 UI的消息到主线程
		hand.sendMessage(message);
	}

	// 刷新UI
	public Handler hand = new Handler() {

		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case Task.TASK_USER_LOGIN:
				System.out.println("---TASK_USER_LOGIN------");
				IWeiboActivity ha = MainService
						.getActivityByName("LoginActivity");
				ha.refresh(LoginActivity.REF_LOGIN_RESULT, msg.obj);
				break;
			//
			case Task.TASK_GET_USER_HOMETIMEINLINE:// 获取我的首页列表
				System.out.println("---TASK_GET_USER_HOMETIMEINLINE------");
				IWeiboActivity hha = MainService
						.getActivityByName("MyListActivity");
				hha.refresh(MyListActivity.REF_WEIBO, msg.obj);
				break;
			// ---------------------罗洪州---------------------

			case Task.TASK_GET_WEIBO_USER:// 更新我的Weibo首页
				System.out.println("---TASK_GET_WEIBO_USER------");
				IWeiboActivity haha = MainService
						.getActivityByName("ListMyWeibosActivity");
				haha.refresh(ListMyWeibosActivity.REF_MYWEIBO, msg.obj);
				break;
			case Task.TASK_GET_MORE_WEIBO_USER:// 更新Weibo首页更多
				System.out.println("---TASK_GET_MORE_WEIBO_USER------");
				IWeiboActivity myhamore = MainService
						.getActivityByName("ListMyWeibosActivity");
				myhamore.refresh(ListMyWeibosActivity.REF_MYWEIBO_MORE, msg.obj);
				break;

			case Task.TASK_GET_MY_TOP:
				System.out.println("---TASK_GET_MY_TOP------");
				IWeiboActivity tp = MainService
						.getActivityByName("ListMyTopicsActivity");
				tp.refresh(ListMyTopicsActivity.REF_MYTOPIC, msg.obj);

				break;
			case Task.TASK_GET_MY_TOP_MORE:

				System.out.println("---TASK_GET_MY_TOP_MORE------");
				IWeiboActivity tp1 = MainService
						.getActivityByName("ListMyTopicsActivity");
				tp1.refresh(ListMyTopicsActivity.REF_MYTOPIC_MORE, msg.obj);
				break;
			// -------------------------------------
			case Task.TASK_GET_USER_HOMETIMEINLINE_MORE:// 更新Weibo首页更多
				System.out
						.println("---TASK_GET_USER_HOMETIMEINLINE_MORE------");
				IWeiboActivity hhamore = MainService
						.getActivityByName("MyListActivity");
				hhamore.refresh(MyListActivity.REF_WEIBO_MORE, msg.obj);
				break;

			case Task.TASK_GET_USER_IMAGE_ICON:// 更新ICON
				System.out.println("----TASK_GET_USER_IMAGE_ICON-----");
				IWeiboActivity ha2 = MainService
						.getActivityByName("HomeActivity");
				if (ha2 != null)
					ha2.refresh(MyListActivity.REF_ICON, msg.obj);
				break;

			case Task.TASK_NEW_WEIBO_COMMENT:// 发表评论
				System.out.println("----TASK_NEW_WEIBO_COMMENT-----");
				MainService.getActivityByName("HomeActivity").refresh(
						MyListActivity.REF_NEW_WEIBO_COMM_RESULT, msg.obj);
				break;

			case Task.TASK_GET_WEIBO_COMMENT:// 当前用户发表的所有评论
				System.out.println("----TASK_GET_WEIBO_COMMENT-----");
				MainService.getActivityByName("MessageActivity")// 
						.refresh(MessageActivity.REF_GET_WEIBO_COMM, msg.obj);
				break;

			case Task.TASK_GET_WEIBO_MESSAGE:// 当前用户发表的所有评论
				System.out.println("----TASK_GET_WEIBO_MESSAGE-----");
				MainService.getActivityByName("MessageActivity").refresh(
						MessageActivity.REF_GET_WEIBO_MESSAGE, msg.obj);
				break;

			case Task.TASK_GET_WEIBO_IMAGE: // 加载微博中的图片
				System.out.println("----TASK_GET_WEIBO_IMAGE-----");
				MainService.getActivityByName("MyListActivity").refresh(
						MyListActivity.REF_GET_WEIBO_IMAGE, msg.obj);
				break;

			case Task.TASK_GET_USER_INFO: // 获取用户详细信息
				System.out.println("----TASK_GET_USER_INFO-----");
				MainService.getActivityByName("UserInfoActivity").refresh(
						UserInfoActivity.REFRESH, msg.obj);
				break;

			case Task.TAST_GET_ATTENTION_INFO:
				System.out.println("--获取好像有的关注信息 -TAST_GET_ATTENTION_INFO-");
				MainService.getActivityByName("ListAllFriendsActivity")
						.refresh(ListAllFriendsActivity.REF_ATTENTION, msg.obj);
				break;
			case Task.TAST_GET_MORE_ATTENTION_INFO: // 获取更多的关注信息
				System.out.println("--获取好像有的关注信息 -TAST_GET_ATTENTION_INFO-");
				MainService.getActivityByName("ListAllFriendsActivity")
						.refresh(ListAllFriendsActivity.REF_MORE_ATTENTION,
								msg.obj);
				break;

			case Task.TASK_GET_USER_IMAGE_ICON_ATTENTION:// 更新ICON
				System.out.println("----TASK_GET_USER_IMAGE_ICON-----");
				MainService.getActivityByName("ListAllFriendsActivity")
						.refresh(ListAllFriendsActivity.REF_ICON, msg.obj);
				break;
			// ???????????????????????????????????????????????
			case Task.TAST_GET_FANS_INFO: // 获取粉丝:
				System.out.println("----//获取粉丝:-----");
				MainService.getActivityByName("ListMyFansActivity").refresh(
						ListMyFansActivity.REF_FANS, msg.obj);
				break;

			case Task.TAST_GET_MORE_FANS_INFO: // 获取粉丝
				System.out.println("----//获取粉丝:-----");
				MainService.getActivityByName("ListMyFansActivity").refresh(
						ListMyFansActivity.REF_MORE_FANS, msg.obj);
				break;

			case Task.TASK_GET_USER_IMAGE_ICON_FANS:
				System.out.println("----//获取粉丝:-----");
				MainService.getActivityByName("ListMyFansActivity").refresh(
						ListMyFansActivity.REF_ICON, msg.obj);
				break;

			case Task.TASK_NEW_WEIBO:// 发表微博
				System.out.println("----TASK_NEW_WEIBO-----");
				IWeiboActivity ha3 = MainService
						.getActivityByName("NewWeiboActivity");
				ha3.refresh(NewWeiboActivity.REF_NEW_WEIBO_RESULT, msg.obj);
				break;

			case Task.TASK_NEW_WEIBO_PIC:// 发表图片微博信息
				MainService.getActivityByName("NewWeiboActivity").refresh(
						NewWeiboActivity.REF_NEW_WEIBO_RESULT, msg.obj);
				break;

			case Task.TASK_NEW_WEIBO_GPS:// 发表GPS微博信息
				MainService.getActivityByName("NewWeiboActivity").refresh(
						NewWeiboActivity.REF_NEW_WEIBO_RESULT, msg.obj);
				break;
			}

		}

	};

	// 提示用户网络异常
	public static void alertNetError(final Activity context) {
		AlertDialog.Builder ab = new AlertDialog.Builder(context);
		// 设定标题
		ab.setTitle(context.getResources().getString(R.string.net_err_title));
		// 设定标题
		ab.setMessage(context.getResources().getString(R.string.net_err_info));
		// 设定退出按钮
		ab.setNegativeButton(context.getResources()
				.getString(R.string.exit_app), new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();
				exitApp(context);
			}
		});
		// 网络设置按钮
		ab.setPositiveButton(
				context.getResources().getString(R.string.net_setting),
				new OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
						context.startActivityForResult(
								new Intent(
										android.provider.Settings.ACTION_WIRELESS_SETTINGS),
								0);
					}
				});
		ab.create().show();
	}

	// 退出应用程序
	public static void exitApp(Activity context) {// 退出所有Activity
		for (int i = 0; i < allActivity.size(); i++) {
			((Activity) allActivity.get(i)).finish();
		}
		allActivity.clear();
		// 退出Service
		context.stopService(new Intent("com.itcast.weibo.mainlogic"));
		// 关闭子线程
		MainService.isrun = false;
		// 关闭广播接口
		MainService.mainService
				.unregisterReceiver(MainService.mainService.netReceiver);

	}

	// 添加任务
	public static void addNewTask(Task ts) {
		allTask.add(ts);
	}

	// 提示是否退出应用程序
	public static void promptExitApp(final Activity context) {
		// 创建对话框
		AlertDialog.Builder ab = new AlertDialog.Builder(context);
		LayoutInflater li = LayoutInflater.from(context);
		View msgview = li.inflate(R.layout.exitdialog, null);
		ab.setView(msgview);// 设定对话框显示的内容
		// ab.setMessage(R.string.comfirm_exit);
		ab.setIcon(R.drawable.ic_help);
		ab.setPositiveButton(R.string.app_exit_ok, new OnClickListener() {
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				arg0.dismiss();
				exitApp(context);// 退出整个应用
			}
		});
		ab.setNegativeButton(R.string.app_exit_cancle, null);
		ab.show();
	}
}
